﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using NDesk.Options;

namespace BMServiceGenerator
{
    public class ArgumentsParser
    {
        private static bool CheckPreParseInput(List<string> args)
        {
            if (args.Exists(a => a.StartsWith("-help")) || args.Exists(a => a.StartsWith("/help")))
            {
                DisplayHelp();
                return false;
            }

            if (!args.Exists(a => a.StartsWith("-definitionfile")) && !args.Exists(a => a.StartsWith("/definitionfile")))
            {
                Console.WriteLine("Could not find a valid defintion file path argument. Use /help for more info.");
                return false;
            }

            if (!args.Exists(a => a.StartsWith("-output")) && !args.Exists(a => a.StartsWith("/output")))
            {
                Console.WriteLine("Could not find a valid output path argument. Use /help for more info.");
                return false;
            }

            if (!args.Exists(a => a.StartsWith("-namespace")) && !args.Exists(a => a.StartsWith("/namespace")))
            {
                Console.WriteLine("Could not find a valid namespace argument. Use /help for more info.");
                return false;
            }

            if (args.Contains(string.Empty) || args.Count < 3)
            {
                Console.WriteLine("Missing argument. Use /help for more info.");
            }

            return true;
        }

        private static bool CheckPostParseInput(Arguments args)
        {
            if (!File.Exists(args.BamDefinitionPath))
            {
                Console.WriteLine(string.Format("Could not find a defintion file in path {0}.", args.BamDefinitionPath));
                return false;
            }

            return true;
        }


        public Arguments Parse(IEnumerable<string> args)
        {
            bool isPostParseValid = false;
            var arguments = new Arguments();

            bool isPreParseValid = CheckPreParseInput(args.ToList());

            if (isPreParseValid)
            {
                OptionSet set = new OptionSet()
                    .Add("definitionfile=", d => arguments.BamDefinitionPath = d)
                    .Add("output=", o => arguments.OutputFolder = o)
                    .Add("namespace=", n => arguments.NamespaceName = n)
                    .Add("help=", h => DisplayHelp());

                set.Parse(args);

                isPostParseValid = CheckPostParseInput(arguments);
            }


            if (!isPreParseValid || !isPostParseValid)
            {
                return null;
            }

            return arguments;
        }

        public static void DisplayHelp()
        {
            FileVersionInfo version = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);
            Console.WriteLine(string.Format("BAM Service Generator Version {0}.{1}", version.FileMajorPart,
                                            version.FileMinorPart));
            Console.WriteLine("Generates a WCF service based on a BizTalk BAM definition file.");
            Console.WriteLine("");
            Console.WriteLine("  -defintionfile: \t Sets path to BAM defintion file.");
            Console.WriteLine("  -output: \t Sets path to output folder.");
            Console.WriteLine("  -namespace: \t Sets namespace to use.");
            Console.WriteLine("");
            Console.WriteLine(
                @"Example: bmsrvgen.exe -defintionfile:c:\MyFiles\MyActivityDef.xml -output:c:\tempservices -nampespace:MyCompanyNamespace");
        }
    }
}